package projet.volleystat;

import projet.volleystat.bdd.VolleyOpenhelper;
import projet.volleystat.bdd.VolleyRepository;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class GestionPlayersActivity extends Activity {
	protected ListView listv;
	protected SimpleCursorAdapter cadapter;
	protected Cursor query;
	protected VolleyRepository volleyDataBase;
	protected String order;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gestion_players);

		volleyDataBase = new VolleyRepository(getApplicationContext());

		volleyDataBase.open();
		order = VolleyOpenhelper.Player_Columns_NAME;

		// Curseur contenant la requête
		try {
			query = volleyDataBase.getAllCursorPlayer(order);
		} catch (DatabaseException e) {
			e.printStackTrace();
		}

		// Mise en place de la listview et remplissage avec la requête
		listv = (ListView) findViewById(R.id.playerslv);
		cadapter = new SimpleCursorAdapter(
				getApplicationContext(),
				R.layout.listitemlineplayer,
				query,
				new String[] { VolleyOpenhelper.Player_Columns_NAME,
						VolleyOpenhelper.Player_Columns_FIRSTNAME,
						VolleyOpenhelper.Player_Columns_NATIONALITY },
				new int[] { R.id.textname, R.id.textfirstname, R.id.textnation });
		registerForContextMenu(listv);
		listv.setAdapter(cadapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.gestion_players, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.addplayer:
			Intent intent = new Intent(getApplicationContext(),
					EditPlayerActivity.class);
			// On passe l'id -1 à l'activité d'édition (signifie ajouter un
			// nouveau joueur)
			intent.putExtra("id", -1);
			intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(intent);
			return true;
		case R.id.sortplayers:
			// PROVISOIRE : rotation entre les différents ordres de tris à la
			// pression du boutton
			if (order == VolleyOpenhelper.Player_Columns_NAME)
				order = VolleyOpenhelper.Player_Columns_FIRSTNAME;
			else if (order == VolleyOpenhelper.Player_Columns_FIRSTNAME)
				order = VolleyOpenhelper.Player_Columns_NATIONALITY;
			else
				order = VolleyOpenhelper.Player_Columns_FIRSTNAME;

			// Rafraichissement de la listview
			query.close();
			try {
				query = volleyDataBase.getAllCursorPlayer(order);
			} catch (DatabaseException e) {
				e.printStackTrace();
			}
			cadapter.changeCursor(query);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.contextmenuplayer, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.delplayer:
			try {
				volleyDataBase.delete(volleyDataBase
						.getPlayerById((int) info.id));
			} catch (DatabaseException e) {
				e.printStackTrace();
			}

			// Rafraichissement de la listview
			query.close();
			try {
				query = volleyDataBase.getAllCursorPlayer(order);
			} catch (DatabaseException e) {
				e.printStackTrace();
			}
			cadapter.changeCursor(query);
			return true;
		case R.id.edtplayer:
			Intent intent = new Intent(getApplicationContext(),
					EditPlayerActivity.class);
			// On passe l'id du joueur choisi à l'activité d'édition
			intent.putExtra("id", (int) info.id);
			intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(intent);
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	@Override
	public void onResume() {
		// Réouverture de la BDD et rafraichissement de la listview lors du
		// retour sur l'activité
		super.onResume();
		volleyDataBase.open();
		try {
			query = volleyDataBase.getAllCursorPlayer(order);
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		cadapter.changeCursor(query);
	}

	@Override
	public void onPause() {
		// Fermeture du curseur et de la BDD lorsque l'actiité est quittée
		super.onPause();
		overridePendingTransition(0, 0);
		query.close();
		volleyDataBase.close();
	}
}
